package hbec.kettle.etl.engine.conf;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import hbec.kettle.etl.common.constants.Constants;
import hbec.kettle.etl.common.exception.ExceptionTracker;
import hbec.kettle.etl.common.util.PropertiesUtils;

public class ParseXMLUtil {
	private static final Logger logger = LoggerFactory.getLogger(ParseXMLUtil.class);

	public static JobConf loadJobConfig(String jobDescXml) {
		JobConf jobConf = new JobConf();
		
		Document documentJobDesc = null;
		try {
			documentJobDesc = DocumentHelper.parseText(jobDescXml);
		} catch (DocumentException e) {
			logger.error(ExceptionTracker.trace(e));
		}
		/*
		Map<String, String> jobFilePaths = new HashMap<>();
		List<?> jobNodes = documentJobDesc.selectNodes("/plugins/job");
		for (Iterator<?> it = jobNodes.iterator(); it.hasNext();) {
			Element e = (Element)it.next();
			jobFilePaths.put(e.attributeValue("name"), e.attributeValue("path"));
		}
		
		Map<String, String> transFilePaths = new HashMap<>();
		List<?> transNodes = documentJobDesc.selectNodes("/plugins/trans");
		for (Iterator<?> it = transNodes.iterator(); it.hasNext();) {
			Element e = (Element)it.next();
			transFilePaths.put(e.attributeValue("name"), e.attributeValue("path"));
		}
		*/
		Map<String, String> fieldsFilePaths = new HashMap<>();
		List<?> fieldsNodes = documentJobDesc.selectNodes("/plugins/fields");
		for (Iterator<?> it = fieldsNodes.iterator(); it.hasNext();) {
			Element e = (Element)it.next();
			fieldsFilePaths.put(e.attributeValue("name"), e.attributeValue("path"));
		}
		
		Document documentJobs = null;
		try {
			String xml = FileUtils.readFileToString(new File(Constants.JOBSXML), "UTF-8");
			documentJobs = DocumentHelper.parseText(xml);
		} catch (DocumentException | IOException e) {
			logger.error(ExceptionTracker.trace(e));
		}
		
		Set<JobEntityConf> jobEntityConfs = new HashSet<>();
		List<?> jobs = documentJobs.selectNodes("/jobs/job");
		for (Iterator<?> it1 = jobs.iterator(); it1.hasNext();) {
			Element e1 = (Element)it1.next();
			
			JobEntityConf jobEntityConf = new JobEntityConf();
			jobEntityConf.setJobName(e1.attributeValue("name"));
			jobEntityConf.setJobLabel(e1.attributeValue("label"));
			jobEntityConf.setJobIdentity(e1.attributeValue("identity"));
			//jobEntityConf.setJobFile(jobFilePaths.get(e1.attributeValue("name")));
			
			List<?> trans = e1.elements("trans");
			Set<TransEntityConf> transEntityConfs = new HashSet<>();
			Set<String> watchTables = new HashSet<>();
			for (Iterator<?> it2 = trans.iterator(); it2.hasNext();) {
				Element e2 = (Element)it2.next();
				
				TransEntityConf transEntityConf = new TransEntityConf();
				transEntityConf.setTransName(e2.attributeValue("name"));
				transEntityConf.setTransSrc(e2.attributeValue("src"));
				transEntityConf.setTransDest(e2.attributeValue("dest"));
				transEntityConf.setTransDepend(e2.attributeValue("depend"));
				//transEntityConf.setTransFile(transFilePaths.get(e2.attributeValue("name")));
				transEntityConf.setTransFieldsFile(fieldsFilePaths.get(e2.attributeValue("name")));
				
				transEntityConfs.add(transEntityConf);
				watchTables.add(e2.attributeValue("src"));
			}
			
			jobEntityConf.setTransEntityConfs(transEntityConfs);
			jobEntityConf.setWatchTables(watchTables);
			
			jobEntityConfs.add(jobEntityConf);
		}
		
		jobConf.setId("hbec");
		jobConf.setJobEntityConfs(jobEntityConfs);
		
		return jobConf;
	}

	public static EngineConf loadEngineConfig() {
		EngineConf ec = EngineConf.getInstance();
		Properties engineProperties = PropertiesUtils.getEngineProperties();
		ec.setSyncLogLevel(engineProperties.getProperty("hbec.kettle.etl.syncLogLevel"));
		ec.setWatchSumTables(engineProperties.getProperty("hbec.kettle.etl.watchSumTables"));
		ec.setCheckPointTable(engineProperties.getProperty("hbec.kettle.etl.checkpointTable"));
		return ec;
	}

	public static Map<String, PluginConf> loadPluginConfig() {
		return new HashMap<>();
	}
	
	public static Map<String, String> loadFieldsConfig(String path) {
		Document documentFields = null;
		try {
			String xml = FileUtils.readFileToString(new File(path), "UTF-8");
			documentFields = DocumentHelper.parseText(xml);
		} catch (DocumentException | IOException e) {
			logger.error(ExceptionTracker.trace(e));
		}
		
		Map<String, String> res = new HashMap<>();
		List<?> fields = documentFields.selectNodes("/fields/field");
		for (Iterator<?> it = fields.iterator(); it.hasNext();) {
			Element e = (Element)it.next();
			res.put(e.attributeValue("from"), e.attributeValue("to"));
		}
		
		return res;
	}
}
